<template>
    <div style="padding:20px;text-align: left;">
        <el-card class="kb-card" shadow="never">
            <div style="display: flex;justify-content: space-between;align-items: center;text-align: left;">
                <h1 class="title">角色列表</h1>
                <el-button type="primary" @click="drawerVisible = true"><el-icon>
                        <Plus />
                    </el-icon>创建角色</el-button>
            </div>
            <div>

                <el-table :data="roleAgentList">
                    <template #empty>
                        <el-empty description="暂无角色，快去创建吧~" />
                    </template>
                    <el-table-column prop="id" label="ID" width="80" />
                    <el-table-column prop="name" label="角色名称" width="180" />
                    <el-table-column prop="character_id" label="角色ID" width="300" />
                    <el-table-column prop="introduction" label="角色说明" />
                    <el-table-column prop="create_time" label="创建时间" />
                    <!-- <el-table-column prop="count" label="知识数量" /> -->
                    <el-table-column fixed="right" label="操作" min-width="120">
                        <template #default="scope">
                            <el-button link type="primary" size="small"
                                @click="() => { this.$router.push(`/roleAgentInfo/${scope.row.id}`) }">
                                详情
                            </el-button>
                            <el-button link type="primary" size="small"
                                @click="deleteRoleAgent(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </el-card>
    </div>

    <el-drawer v-model="drawerVisible" title="新增角色" :direction="direction" :before-close="handleClose"
        style="text-align: left;">

        <el-form label-position="top">
            <el-form-item label="角色名称">
                <el-input placeholder="请输入角色名称。美髯公关云长" v-model="form.name" />
            </el-form-item>
            <el-form-item label="角色介绍">
                <el-input placeholder="请输入角色介绍。满腔忠义的关云长" v-model="form.introduction" />
            </el-form-item>
            <el-form-item label="角色Prompt">
                <el-input placeholder="请输入角色Prompt。我们来玩一个角色扮演的游戏， 你是「满腔忠义的关云长」。" v-model="form.basic_information" />
            </el-form-item>
            <el-form-item label="开场白">
                <el-input placeholder="请输入角色开场白。我是「关云长」，很高心与你玩游戏" v-model="form.opening_line" />
            </el-form-item>
            <el-form-item label="特殊要求">
                <el-input placeholder="请输入角色特殊要求。请在对话时尽可能的展现你的性格、感情， 用文言文回答， 并使用古人的语气和用词。" v-model="form.traits" />
            </el-form-item>
            <el-form-item label="任务目标">
                <el-input placeholder="请输入角色任务目标。我要关羽拜把子。" v-model="form.chat_objective" />
            </el-form-item>
            <el-form-item label="角色音色">
                <el-select placeholder="请选择角色音色。" v-model="form.sound">
                    <el-option label="女孩" value="女孩" />
                </el-select>
            </el-form-item>
        </el-form>

        <template #footer>
            <div style="flex: auto">
                <el-button @click="drawerVisible = false">取消</el-button>
                <el-button type="primary" @click="createRoleAgent">确认</el-button>
            </div>
        </template>
    </el-drawer>
</template>

<script>
export default {
    data() {
        return {
            roleAgentList: [],
            drawerVisible: false,
            direction: 'rtl',
            form: { basic_information: "", name: "", introduction: "", opening_line: "", traits: "", chat_objective: '', sound: "女孩", id: 0 },
        }
    },
    methods: {
        async getRoleAgentList() {
            try {
                const response = await fetch(`${import.meta.env.VITE_API_URL}/roleAgent/getRoleAgentList`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({})
                });
                if (response.status === 401) {
                    localStorage.removeItem('access_token');
                    this.$router.push('/login');
                    return;
                }
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                const rs = await response.json();
                console.log("rs:>>:", rs);
                this.roleAgentList = rs.data;
            } catch (error) {
                console.error('There was a problem with the fetch operation:', error);
            }
        },
        async createRoleAgent() {
            try {
                const response = await fetch(`${import.meta.env.VITE_API_URL}/roleAgent/createRoleAgent`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(this.form)
                });
                if (response.status === 401) {
                    localStorage.removeItem('access_token');
                    this.$router.push('/login');
                    return;
                }
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                const rs = await response.json();
                console.log("rs:>>:", rs);
                this.drawerVisible = false;
                this.getRoleAgentList();
            } catch (error) {
                console.error('There was a problem with the fetch operation:', error);
            }
        },
        async deleteRoleAgent(row) {
            try {
                const response = await fetch(`${import.meta.env.VITE_API_URL}/roleAgent/deleteRoleAgent`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(row)
                });
                if (response.status === 401) {
                    localStorage.removeItem('access_token');
                    this.$router.push('/login');
                    return;
                }
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                const rs = await response.json();
                console.log("rs:>>:", rs);
                this.getRoleAgentList();
            } catch (error) {
                console.error('There was a problem with the fetch operation:', error);
            }
        },
        handleClose(done) {
            this.$confirm('确定要关闭吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                done();
            }).catch(() => { });
        }
    },
    mounted() {
        this.getRoleAgentList();
    }
}
</script>

<style></style>
